#import "drawundo.h"

/*
 * This change is created when the user finishes editing a text
 * graphic. Undoing this change inserts the re-inserts the text
 * editing cursor in the text. More significantly, undoing this
 * change swaps the contents of the TextGraphic back into the
 * field editor so that it is ready to edit.
 */

@interface EndEditingGraphicsChange(PrivateMethods)

@end

@implementation EndEditingGraphicsChange

- initGraphicView:aGraphicView graphic:aGraphic
{
    [super init];
    graphicView = aGraphicView;
    graphic = aGraphic;

    return self;
}

- free
{
    if ([self hasBeenDone] && [graphic isEmpty])
        [graphic free];
    return [super free];
}

- (const char *)changeName
{
    return NXLocalStringFromTable("Operations", "End Editing", NULL, "The operation of ending the editing process and proceding onto some other operation.");
}

- undoChange
{
    if ([graphic isEmpty])
	[graphicView insertGraphic:graphic];
    [graphic prepareFieldEditor];    
    [NXApp startEditMode];
    return [super undoChange];
}

- redoChange
{
/* 
 * The order of the next two statements is important.
 * If endEditMode were sent before resignFieldEditor 
 * it would send resetCursor to the document which would
 * make the window the first responder which would end
 * up sending textDidEnd:endChar: to the TextGraphic.
 * Then in the next line we'd send resignFieldEditor to
 * the TextGraphic even though it had already resigned 
 * the field editor.
 */
    [graphic resignFieldEditor];
    [NXApp endEditMode];
    if ([graphic isEmpty])
	[graphicView removeGraphic:graphic];
    return [super redoChange];
}

@end
